<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="品番编码">
              <a-input v-model="queryParam.skuCode" placeholder="请输入品番编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="货位">
              <a-input v-model="queryParam.location" placeholder="请输入货位"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :lg="7" :md="8" :sm="24" :xl="6">
              <a-form-item label="状态">
                <j-dict-select-tag v-model="queryParam.status" dictCode="check_status"
                                   placeholder="请选择到货状态" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
                        <span class="table-page-search-submitButtons"
                              style="float: left;overflow: hidden;">
                            <a-button icon="search" type="primary"
                                      @click="searchQuery">查询</a-button>
                            <a-button icon="reload" style="margin-left: 8px" type="primary"
                                      @click="searchReset">重置</a-button>
                            <a style="margin-left: 8px" @click="handleToggleSearch">
                                {{ toggleSearchStatus ? '收起' : '展开' }}
                                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                            </a>
                        </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="plus" type="primary" @click="handleAdd">新增</a-button>
      <a-button icon="download" type="primary" @click="handleExportXls('pt盘点表')">导出</a-button>
      <a-upload :action="importExcelUrl" :headers="tokenHeader" :multiple="false" :showUploadList="false"
                name="file" @change="handleImportExcel">
        <a-button icon="import" type="primary">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query ref="superQueryModal" :fieldList="superFieldList"
                     @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pagination="ipagination"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :scroll="{x:true}"
        bordered
        class="j-table-force-nowrap"
        rowKey="id"
        size="middle"
        @change="handleTableChange">

        <template slot="checkNumber" slot-scope="text, record">
          <a-input v-if="record.status == 1" v-model="record.checkNumber" placeholder="请输入盘点数量"
                   style="width: 90%;" type="number" />
          <a v-if="record.status == 1" @click="handleCheck(record)">确认</a>
          <div v-else v-html="text"></div>
        </template>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" alt="" height="25px"
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            icon="download"
            size="small"
            type="primary"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-has="'user:super'">编辑</a>
          <a-divider type="vertical" v-has="'user:super'"/>
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical" v-has="'user:super'"/>
          <a-popconfirm v-has="'user:super'" title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider v-if="record.status == 2" v-has="'user:super'" type="vertical" />
          <a v-if="record.status == 2" v-has="'user:super'" @click="sycn(record)">处理差异</a>
<!--          <a-dropdown>-->
<!--            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>-->
<!--            <a-menu slot="overlay">-->
<!--              <a-menu-item>-->
<!--              </a-menu-item>-->
<!--              <a-menu-item >-->

<!--              </a-menu-item>-->
<!--              <a-menu-item>-->

<!--              </a-menu-item>-->
<!--            </a-menu>-->
<!--          </a-dropdown>-->
        </span>

      </a-table>
    </div>

    <check-inventory-pt-modal ref="modalForm" @ok="modalFormOk"></check-inventory-pt-modal>
    <a-modal title="差异原因" :visible="showSycn" :confirm-loading="sycnLoading" @ok="sycnOk"
             @cancel="sycnCancel">
      <a-textarea placeholder="请输入差异原因" v-model="reason"
                  :auto-size="{ minRows: 3, maxRows: 5 }" />
    </a-modal>
  </a-card>
</template>

<script>

import '@assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import CheckInventoryPtModal from './modules/CheckInventoryPtModal.vue'
import { postAction } from '../../api/manage';

export default {
  name: 'CheckInventoryPtList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    CheckInventoryPtModal
  },
  data() {
    return {
      description: 'pt盘点表管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '品番编码',
          align: 'center',
          dataIndex: 'skuCode'
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'number'
        },
        {
          title: '货位',
          align: 'center',
          dataIndex: 'location'
        },
        {
          title: '盘点数量',
          align: "center",
          dataIndex: 'checkNumber',
          scopedSlots: { customRender: 'checkNumber' }
        },
        {
          title: '盘点结果',
          align: 'center',
          dataIndex: 'result_dictText'
        },
        {
          title: '差异数量',
          align: "center",
          dataIndex: 'differentNumber'
        },
        {
          title: '盘点状态',
          align: 'center',
          dataIndex: 'status_dictText'
        },
        {
          title: '盘点人',
          align: 'center',
          dataIndex: 'checkMan'
        },
        {
          title: '处理原因',
          align: 'center',
          dataIndex: 'reason'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/checkInventoryPt/checkInventoryPt/list',
        delete: '/checkInventoryPt/checkInventoryPt/delete',
        deleteBatch: '/checkInventoryPt/checkInventoryPt/deleteBatch',
        exportXlsUrl: '/checkInventoryPt/checkInventoryPt/exportXls',
        importExcelUrl: 'checkInventoryPt/checkInventoryPt/importExcel',
        handleCheck: "/checkInventoryPt/checkInventoryPt/handleCheck",
        sycn: "/checkInventoryPt/checkInventoryPt/sycn"

      },
      dictOptions: {},
      superFieldList: [],
      showSycn: false,
      sycnLoading: false,
      reason: undefined,
      checkInventory: undefined,
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    sycn(record) {
      this.showSycn = true
      this.checkInventory = record
    },
    sycnOk(e) {
      if (!this.reason) {
        this.$message.error("请输入原因")
        return
      }
      this.checkInventory.reason = this.reason
      this.sycnLoading = true
      postAction(this.url.sycn, this.checkInventory).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.showSycn = false
        this.sycnLoading = false
        this.reason = undefined
        this.checkInventory = undefined
      });
    },
    sycnCancel(e) {
      this.showSycn = false;
    },
    handleCheck(record) {
      this.loading = true;
      postAction(this.url.handleCheck, record).then((res) => {
        if (res.success) {
          this.$message.success(res.message);
          this.loadData();
        } else {
          this.$message.warning(res.message);
        }
      }).finally(() => {
        this.loading = false;
      });
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'skuCode', text: '品番编码', dictCode: '' })
      fieldList.push({ type: 'int', value: 'number', text: '数量', dictCode: '' })
      fieldList.push({ type: 'string', value: 'location', text: '货位', dictCode: '' })
      fieldList.push({ type: 'int', value: 'checkNumber', text: '盘点数量', dictCode: '' })
      fieldList.push({ type: 'int', value: 'result', text: '盘点结果', dictCode: '' })
      fieldList.push({ type: 'int', value: 'status', text: '盘点状态', dictCode: '' })
      fieldList.push({ type: 'double', value: 'boxNum', text: '箱数', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>